<?php

namespace app\api\model;

use think\Model;

class User extends Model
{
    /*** 用户列表
     * @param $where
     * @param $pageIndex
     * @param $pageSize
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userList($where,$pageIndex,$pageSize){
        return self::where($where)->order('uid desc')->page($pageIndex,$pageSize)->select();
    }


    /*** 注册
     * @param $data
     * @return User|Model
     */
    public static function registered($data){
        return self::insertGetId($data);
    }

    /*** 账号信息修改
     * @param $where
     * @param $info
     * @return User
     */
    public static function userEdit($where,$info){
        return self::update($info,$where);
    }

    /*** 修改密码
     * @param $user
     * @param $newPassword
     * @return User
     */
    public static function forgetPassword($user,$newPassword){
        return self::where('uid',$user['uid'])->update(['password'=>self::encPassword($newPassword,$user['encryption'])]);
    }

    /*** 修改密码
     * @param $user
     * @param $newPassword
     * @return User
     */
    public static function forgetPayPassword($user,$newPayPassword){
        return self::where('uid',$user['uid'])->update(['pay_password'=>self::encPassword($newPayPassword,$user['encryption'])]);
    }


    /** 通过特定条件获取用户信息
     * @param $where
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userInfo($where){
        return self::where($where)->find();
    }

    /*** 通过用户id 获取用户信息
     * @param $uid
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userIdInfo($uid){
        return self::where('uid',$uid)->find();
    }


    /** 查询用户某个字段值
     * @param $where
     * @param $field
     * @return string
     */
    public static function valInfo($where,$field = "uid"){
        $info = self::where($where)->value($field);
        return $info;
    }

    /** 新用户授权注册
     * @param $where
     * @param $field
     * @return string
     */
    public static function addNewUser($data){
        return self::insertGetId($data);
    }

    public static function edit($where,$data){
        return self::where($where)->update($data);
    }


    /*** 增加用户购买店铺数量
     * @param $userId
     * @param $num
     * @return mixed
     */
    public static function userPayShopNum($userId,$num){
        return self::where('uid',$userId)->inc('pay_shop_num', $num)->update();
    }


    /*** 获取一定条件下的用户数量
     * @param $where
     * @return int
     */
    public static function userCount($where){
        return self::where($where)->count();
    }


    public static function userMPCount($where){
        return self::field('sum(if(type=2,1,0)) as merchant_count,sum(if(type=3,1,0)) as user_count')->where($where)->find();
    }


    public static function userInc($where,$field){
        return self::where($where)->inc($field, 1)->update();
    }


    /** 通过特定条件获取用户信息
     * @param $where
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userInfoRaw($where){
        return self::whereRaw($where)->find();
    }



    /**
     * 生成随机字符串
     * @param int $length
     * @return string
     */
    public static function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //密码加密
    public static function encPassword($password,$encryption){
        $enc = md5(md5($password).$encryption);
        return $enc;
    }

    //用户数量
    public static function userSelect($where){
        return self::where($where)->select();
    }

    public static function userBankSelect($where){
        return self::field('u.uid,u.phone,b.bank_card,b.account_holder')
            ->alias('u')
            ->leftJoin('pr_bank b','u.uid = b.uid')
            ->where($where)
            ->find();
    }


    public static function ruleSql($sql){
        return self::query($sql);
    }
}